package com.cynteka.cyfoman.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * User: Sergey
 * Date: 10.08.11
 * Time: 17:05
 */
public class RoleFilterList extends ArrayList {

    public RoleFilterList(Collection c, RoleProvider roleProvider) {
        super(filter(c, roleProvider));

    }

    private static Collection filter(Collection c, RoleProvider roleProvider) {
        List res = new ArrayList();
        for(Object o : c) {
            if(o instanceof RoleFilter) {
                RoleFilter roleFilter = (RoleFilter) o;
                if(roleProvider.hasAtLeastOneRole(roleFilter.getRoles())) {
                    res.add(roleFilter.getWrapppedObject());
                }
            } else {
                res.add(o);
            }

        }
        return res;
    }
}
